﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace OneOnOneRunnerConsole
{
    /// <summary>
    /// The class is used to write messages to output streams (either a file, a console or both);
    /// </summary>
    class IO
    {
        /// <summary>
        /// An enum representing possible outputs
        /// </summary>
        public enum WriteTo {Console, File, ConsoleAndFile};

        /// <summary>
        /// the FileInfo and the StreamWriter objects are used to access a file
        /// </summary>
        private static FileInfo file;
        private static StreamWriter fileStream;

        /// <summary>
        /// Writes messages to a specified output
        /// </summary>
        /// <param name="text">
        /// the message
        /// </param>
        /// <param name="where">
        /// Where to write the message to (Console only, File only or both)
        /// </param>
        public static void Write(string text, WriteTo where)
        {
            try
            {
                file = new FileInfo("RunResults.txt");
                fileStream = file.AppendText();
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
            if (where == WriteTo.Console || where == WriteTo.ConsoleAndFile)
            {
                Console.Write(text);
            }
            if (where == WriteTo.File || where == WriteTo.ConsoleAndFile)
            {
                try
                {
                    fileStream.Write(text);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            try
            {
                fileStream.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
        }

        /// <summary>
        /// Same as "Write" function, only adds an end of line marker
        /// </summary>
        public static void WriteLine(string text, WriteTo where)
        {
            try
            {
                file = new FileInfo("RunResults.txt");
                fileStream = file.AppendText();
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
            if (where == WriteTo.Console || where == WriteTo.ConsoleAndFile)
            {
                Console.WriteLine(text);
            }
            if (where == WriteTo.File || where == WriteTo.ConsoleAndFile)
            {
                try
                {
                    fileStream.WriteLine(text);
                }
                catch (IOException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            try
            {
                fileStream.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
        }

        /// <summary>
        /// Create a new file (overwrite if file already exists)
        /// </summary>
        public static void createResources()
        {
            try
            {
                file = new FileInfo("RunResults.txt");
                fileStream = file.CreateText();
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                fileStream.Close();
            }
        }
    }
}
